<template>
  <div class="publicCourse">
    <div class="searchBox">
      <el-button type="primary" icon="el-icon-plus" @click="addPublic">新增</el-button>
      <div class="searchCon">
        <div class="search">
          <span>学院</span>
          <el-input v-model="department" size="medium" class="searchInput"></el-input>
        </div>
        <div class="search">
          <span>课程号</span>
          <el-input v-model="courseId" size="medium" class="searchInput"></el-input>
        </div>
        <div class="search">
          <span>课程名称</span>
          <el-input v-model="courseName" size="medium" class="searchInput"></el-input>
        </div>
        <div class="search">
          <span>教师工号</span>
          <el-input v-model="teacherId" size="medium" class="searchInput"></el-input>
        </div>
        <el-button type="primary" icon="el-icon-search" size="small" style="margin-left: 20px;" @click="getList('search')">搜索</el-button>
      </div>
    </div>
    <BasicTablePagination @handler="tableHandler" :column="column" :data="data" :pagination="pagination" @sizeChange="sizeChange" @numChange="numChange"></BasicTablePagination>
    <editInfoDialog ref="editInfoDialogRef" @reloadData="getList('reload')"></editInfoDialog>
  </div>
</template>

<script>
import BasicTablePagination from '@/components/BasicTablePagination/BasicTablePagination'
import { tableColumn } from "@/constants/publicCourseTable"
import { publicCourse, deletePublicCou } from "@/api/courseManage"
import editInfoDialog from './components/editInfoDialog'


export default {
  name: "index",
  components: {
    BasicTablePagination,
    editInfoDialog
  },
  data() {
    return {
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      data: [
        {
          "courseId": 191017,
          "department": "能源学院",
          "teacherId": "19981001",
          "courseName": "燃气安全技术",
          "credit": 1.5,
          "courseAttribute": "选修",
          "courseTime": "周一3-4节",
          "courseLocation": "西配301",
          "capacity": 142,
          "selectionStartTime": "2023-03-26 16:18:32",
          "selectionEndTime": "2023-05-14 16:19:22",
          "name": "岑平源"
        }
      ],
      column: tableColumn,
      department: '',
      courseId: '',
      courseName: '',
      teacherId: ''
    }
  },
  methods: {
    // 表格处理方法
    tableHandler(val) {
      let { type, data } = val
      console.log(type, data)
      switch(type) {
        case 'edit':
          this.editInfo(data)
          break
        case 'delete':
          this.deleteData(data)
          break
        default:
          break
      }
    },
    async getList(val) {
      let params = {
        pageNum: this.pagination.pageNum,
        pageSize: this.pagination.pageSize,
        department: this.department,
        courseName: this.courseName,
        teacherId: this.teacherId,
        courseId: this.courseId
      }
      if (val == 'search') {
        params.pageNum = 1
        params.pageSize = 10
      }
      let res = await publicCourse(params)
      if (res.success == true) {
        this.data = res.data
        this.pagination.total = res.total
      } else {
        this.$message.error(res.errorMsg)
      }
    },
    // 分页大小改变
    sizeChange(val) {
      this.pagination.pageSize = val
      this.getList('reload')
    },
    // 分页改变
    numChange(val) {
      this.pagination.pageNum = val
      this.getList('reload')
    },
    // 编辑信息
    editInfo(data) {
      this.$refs.editInfoDialogRef.openDialog(data)
    },
    // 删除
    deleteData(val) {
      this.$confirm('是否删除该公共选修课？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await deletePublicCou(val.courseId)
        if (res.success == true) {
          await this.getList('reload')
          this.$message.success('删除成功！')
        } else {
          this.$message.error(res.errorMsg)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增公共选修课
    addPublic() {
      this.$refs.editInfoDialogRef.openDialog({})
    }
  },
  created() {
    this.getList('load')
  }
}
</script>

<style scoped lang="sass">
.publicCourse
  padding: 20px
  .searchBox
    display: flex
    align-items: center
    justify-content: space-between
    margin-bottom: 20px
    .searchCon
      display: flex
      align-items: center
      justify-content: flex-end
      .search
        display: inline-flex
        align-items: center
        justify-content: center
        margin-right: 10px
        span
          width: 80px
        .searchInput
          width: 150px
</style>
